﻿using DevExpress.ExpressApp.ApplicationBuilder;
using DevExpress.ExpressApp.ApplicationBuilder.Internal;
using DevExpress.ExpressApp.DC;
using Microsoft.Extensions.DependencyInjection;

namespace EasyXaf.ObjectSpaces.OData;

public static class ODataObjectSpaceProviderBuilderExtensions
{
    public static IObjectSpaceProviderServiceBasedBuilder<TContext> AddOData<TContext>(this IObjectSpaceProviderServiceBasedBuilder<TContext> builder, string serviceUri)
        where TContext : IXafApplicationBuilder<TContext>, IAccessor<IServiceCollection>
    {
        return builder.Add(serviceProvider =>
        {
            var requiredService = serviceProvider.GetRequiredService<ITypesInfo>();
            Uri serviceRoot = null;
            if (!string.IsNullOrWhiteSpace(serviceUri))
            {
                serviceRoot = new Uri(serviceUri);
            }
            return new ODataObjectSpaceProvider(serviceProvider, requiredService, null, serviceRoot);
        });
    }
}
